package cn.imvc.app.models;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mortbay.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.sd4324530.fastweixin.api.TemplateMsgAPI;
import com.github.sd4324530.fastweixin.api.entity.TemplateMsg;
import com.github.sd4324530.fastweixin.api.entity.TemplateParam;

import cn.imvc.app.commons.SMSDomain;
import cn.imvc.app.entities.Member;
import cn.imvc.util.Wechat;

@Service
public class MessageModelImpl implements MessageModel {

	private Logger log = Logger.getLogger(MessageModelImpl.class);
	
	@Autowired
	SMSModel smsModel;
	
	@Autowired
	Wechat wechat;
	
	@Override
	public boolean sendMessage(Member member, String type, List<String> param) {
		String code = null;
		if (member.getWxOpenid() == null||"".equals(member.getWxOpenid())) {
			switch (type) {
			case "newMember":
				code = "122154";
				break;
			case "deliver":
				code = "109884";
				break;
			}
			List<String> list=new ArrayList<String>();
			for(String tmp:param){
				list.add(tmp);
			}
			try{
				smsModel.sendMessageCommon(member.getMobile(), code, list);
				}catch(Exception e){
					log.error("phone:member"+member.getId()+"type"+type+e.toString());
				}
		} else {
			Map<String, TemplateParam> data=new HashMap<String, TemplateParam>();
			switch (type) {
			case "newMember":
				code = "3eiMHSOzWdKDk598RAw2FwGx_phSItw8FsmPOH4wrnI";
				TemplateParam ap1=new TemplateParam();
				ap1.setValue("您好，您新成员加入");
				ap1.setColor("#000000");
				data.put("first", ap1);
				
				TemplateParam ap2=new TemplateParam();
				ap2.setValue(param.get(1));
				ap2.setColor("#000000");
				data.put("keyword1", ap2);
				
				Date currentTime = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateString = formatter.format(currentTime);
				TemplateParam ap3=new TemplateParam();
				ap3.setValue(dateString);
				ap3.setColor("#000000");
				data.put("keyword2", ap3);
				
				break;
			case "deliver":
				code = "WuSSknlHhhXESVZyymFRNxGY-l0lF1Cq9rVxB79wejg";
				
				TemplateParam bp1=new TemplateParam();
				bp1.setValue("亲，宝贝已经启程了，好想快点来到你身边");
				bp1.setColor("#000000");
				data.put("first", bp1);
				
				TemplateParam bp2=new TemplateParam();
				bp2.setValue(param.get(0));
				bp2.setColor("#000000");
				data.put("delivername", bp2);
				
				TemplateParam bp3=new TemplateParam();
				bp3.setValue(param.get(1));
				bp3.setColor("#000000");
				data.put("ordername", bp3);
				
				TemplateParam bp4=new TemplateParam();
				bp4.setValue("如有疑问，请联系客服4001381101");
				bp4.setColor("#000000");
				data.put("remark", bp4);
				
				
				break;
			}
			TemplateMsgAPI api=new TemplateMsgAPI(wechat.getApiConfig()) ;
			TemplateMsg msg=new TemplateMsg();
			msg.setTouser(member.getWxOpenid());
			msg.setTemplateId(code);
			msg.setData(data);
			msg.setTopcolor("#000000");
			msg.setUrl("");
			try{
			api.send(msg).toString();
			}catch(Exception e){
				log.error("weixin:member"+member.getId()+"type"+type+e.toString());
			}
		}
		return true;
	}
}
